﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;
using System.IO;
namespace Urd.Common
{
    public class config_sql
    {
    
        private static SQLiteConnection scon;
        private static string sqlshema = "CREATE TABLE if not exists options (name varchar PRIMARY KEY,value varchar)";
        private static string config_file_path = "server_sql.cfg";

        public static string server_bind_ip_adress_or_domain 
        {
            get 
            {
                return get("server_bind_ip_adress_or_domain");
            }
            set 
            {
                set("server_bind_ip_adress_or_domain",value);
            }
        }
        public static string server_bind_port
        {
            get 
            {
                return get("server_bind_port");
            }
            set 
            {
                set("server_bind_port", value);
            }
        }
        
        public static string shemasfolder=@"shemas\";
        public static string tablesfolder=@"tables\";
        public static string workersfolder=@"workers\";

        public static string database_ip_adress_or_domain
        {
            get
            {
                return get("database_ip_adress_or_domain");
            }
            set
            {
                set("database_ip_adress_or_domain", value);
            }
        }
        public static string database_port
        {
            get
            {
                return get("database_port");
            }
            set
            {
                set("database_port", value);
            }
        }
        public static string database_name
        {
            get
            {
                return get("database_name");
            }
            set
            {
                set("database_name", value);
            }
        }
        public static string database_username
        {
            get
            {
                return get("database_username");
            }
            set
            {
                set("database_username", value);
            }
        }
        public static string database_password {get{return get("database_password");}set{set("database_password", value);}}

        static config_sql() 
        {
            
          //  try
         //   {
                //l.DoFile(config_file_path);
            scon = new SQLiteConnection();
                scon.ConnectionString="Data Source=:memory:;Version=3;New=True;";
                scon.Open();
                Sql(sqlshema);
          //  }
           // catch (LuaInterface.Exceptions.LuaScriptException e) { debug.Log("[luascript]:" + e.Source + " " + e.Message,debug.loglevel.Errors); }
           // catch (LuaInterface.Exceptions.LuaException e) { debug.Log("[lua]:" + e.Source + " " + e.Message, debug.loglevel.Errors); }
           // catch (Exception e) { debug.Log("[system]:" + e.Source + " " + e.Message, debug.loglevel.Errors); }
         //   finally
         //   {
           //     scon.Close();
              //  scon.Dispose();
           // }
        }
        public static void save()
        {
            
        }
        public static void load() 
        {
            
        }

        private static string Sql(string request)
        {
            try
            {
                SQLiteCommand scom = new SQLiteCommand(scon);
                scom.CommandText = request;
                return scom.ExecuteScalar().ToString();
            }
            catch (Exception e) { return e.Message; }

        }
        private static void set(string n, string v)
        {
            Sql("insert or replace into options values(`" + n + "`,`" + v+"`)");
        }
        private static string get(string w)
        {
            return Sql("select value from options where name ="+w);
        }


           
    }
}

